import mockjs from 'mockjs';
import { delay } from 'roadhog-api-doc';
import { parse } from 'url';
import { pagination } from '@/utils/utils'

// *****************************************************************************************
// * 系统日志 -登录日志
// *****************************************************************************************
let logLoginList = [];
for (let i = 0; i < 46; i += 1) {
  logLoginList.push({
    id: i,
    userId: 1,
    username: `张三`,
    logined: new Date(`2019-08-${Math.floor(i / 2) + 1}`),
    ip: `0.0.0.${i}`,
    location: '位置 位置 位置',
    terminal: 'Chrome 1.2.3',
    platform: 'Windows 7',
  });
}

function getLogLogin(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }
  const params = parse(url, true).query;

  let dataSource = logLoginList;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.username) {
    dataSource = dataSource.filter(data => data.username.indexOf(params.username) > -1);
  }

  if (params.loginedSt && params.loginedEd) {
    dataSource = dataSource.filter(
      data => data.logined >= params.loginedSt && data.logined <= params.loginedEd
    );
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: pagination(parseInt(params.currentPage, 10) || 1, pageSize, dataSource),
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  console.log('Mock: got login logs');
  return res.status(200).json({
    code: 200,
    data: result,
  });
}

// *****************************************************************************************
// * 系统日志 -操作日志
// *****************************************************************************************
let logOperationList = [];
for (let i = 0; i < 46; i += 1) {
  logOperationList.push({
    id: i,
    userId: 1,
    username: `张三`,
    operated: new Date(`2019-08-${Math.floor(i / 2) + 1}`),
    action: '新增',
    object: '用户',
    operation: '{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx }',
    ip: `0.0.0.${i}`,
    location: '位置 位置 位置',
    terminal: 'Chrome 1.2.3',
    platform: 'Windows 7',
  });
}

function getLogOperation(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }
  const params = parse(url, true).query;

  let dataSource = logOperationList;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.username) {
    dataSource = dataSource.filter(data => data.username.indexOf(params.username) > -1);
  }

  if (params.action) {
    dataSource = dataSource.filter(data => data.action === params.action);
  }

  if (params.object) {
    dataSource = dataSource.filter(data => data.object === params.object);
  }

  if (params.operatedSt && params.operatedEd) {
    dataSource = dataSource.filter(
      data => data.operated >= params.operatedSt && data.operated <= params.operatedEd
    );
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: pagination(parseInt(params.currentPage, 10) || 1, pageSize, dataSource),
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  console.log('Mock: got operation logs');
  return res.status(200).json({
    code: 200,
    data: result,
  });
}

// *****************************************************************************************
// * 匹配
// *****************************************************************************************
const proxy = {
  'GET /api/ls/log/login': getLogLogin,
  'GET /api/ls/log/operation': getLogOperation,
};

// 调用 delay 函数，统一处理
export default delay(proxy, 2000);